﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Pixysoft.Tools;

namespace Pixysoft.Framework.Noebe.Core
{
    class EntityNoebeCommand : IEntityNoebeCommand
    {
        INoebeCommand command = null;

        public EntityNoebeCommand(INoebeCommand command)
        {
            this.command = command;
        }

        public INoebeCommand Command
        {
            get
            {
                return command;
            }
        }

        public bool IsOpen
        {
            get { return command.IsOpen; }
        }

        public string SQL
        {
            get
            {
                return command.SQL;
            }
            set
            {
                command.SQL = value;
            }
        }

        public void Begin()
        {
            command.Begin();
        }

        public void Begin(System.Data.IsolationLevel level)
        {
            command.Begin(level);
        }

        public void Begin(NoebeTransactionArea transactionArea)
        {
            command.Begin(transactionArea);
        }

        public void Begin(System.Data.IsolationLevel level, NoebeTransactionArea transactionArea)
        {
            command.Begin(level, transactionArea);
        }

        public NoebeParameterCollection Parameters
        {
            get { return command.Parameters; }
        }

        public List<T> ExecuteReader<T>()
        {
            DataTable table = command.ExecuteReader();

            return EntityHelper.DataTableToEntity<T>(table);
        }

        public int Execute()
        {
            return command.Execute();
        }

        public void Commit()
        {
            command.Commit();
        }

        public void Rollback()
        {
            command.Rollback();
        }



        public List<T> Select<T>() where T : IEntityBase
        {
            String tableName = EntityHelper.GetTableName(typeof(T));

            DataTable table = command.Select(tableName);

            if (table.Rows.Count == 0)
                return new List<T>();

            return EntityHelper.DataTableToEntity<T>(table);
        }

        public T SelectByPk<T>(params object[] values) where T : IEntityBase
        {
            String tableName = EntityHelper.GetTableName(typeof(T));

            DataTable table = command.SelectByPk(tableName, values);

            if (table.Rows.Count == 0)
                return default(T);

            List<T> entities = EntityHelper.DataTableToEntity<T>(table);

            if (entities.Count == 0)
                return default(T);

            return entities[0];
        }



        public int Insert(params IEntityBase[] entities)
        {
            foreach (IEntityBase entity in entities)
            {
                entity.CreateDate = GlobalTimer.Instance.GetGlobalTime();
                entity.ModiDate = GlobalTimer.Instance.GetGlobalTime();
            }

            DataTable table = EntityHelper.EntityToDataTable(entities);

            if (table == null)
                return 0;

            return command.Insert(table);
        }

        public int AutoInsert(params IEntityBase[] entities)
        {
            foreach (IEntityBase entity in entities)
            {
                entity.CreateDate = GlobalTimer.Instance.GetGlobalTime();
                entity.ModiDate = GlobalTimer.Instance.GetGlobalTime();
            }

            DataTable table = EntityHelper.EntityToDataTable(entities);

            if (table == null)
                return 0;

            return command.AutoInsert(table);
        }

        public int Update(params IEntityBase[] entities)
        {
            foreach (IEntityBase entity in entities)
            {
                entity.ModiDate = GlobalTimer.Instance.GetGlobalTime();
            }

            DataTable table = EntityHelper.EntityToDataTable(entities);

            if (table == null)
                return 0;

            return command.Update(table);
        }

        public int Delete(params IEntityBase[] entities)
        {
            DataTable table = EntityHelper.EntityToDataTable(entities);

            if (table == null)
                return 0;

            return command.Delete(table);
        }

        public void CascadeDelete(params IEntityBase[] entities)
        {
            DataTable table = EntityHelper.EntityToDataTable(entities);

            if (table == null)
                return;

            command.CascadeDelete(table);
        }



        public void Dispose()
        {
            command.Dispose();
            this.command = null;
        }

    }
}
